package com.excesys.exsecs.energy.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.energy.entity.EnergyParamManage;
import com.excesys.exsecs.energy.entity.EnergyParamStandard;
import com.excesys.exsecs.energy.mapper.EnergyParamManageMapper;
import com.excesys.exsecs.energy.service.EnergyParamManageService;
import com.excesys.exsecs.energy.service.EnergyParamStandardService;
import com.excesys.exsecs.energy.vo.EnergyParamManageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 能源参数配置
 *
 * @author zhang
 * @date 2024-09-26 10:29:19
 */
@Service
public class EnergyParamManageServiceImpl extends ServiceImpl<EnergyParamManageMapper, EnergyParamManage> implements EnergyParamManageService {

    @Autowired
    private EnergyParamStandardService energyParamStandardService;

    @Override
    public EnergyParamManageVo queryAndEnergy() {
        EnergyParamManageVo manageVo = new EnergyParamManageVo();
        EnergyParamManage paramManage = this.baseMapper.selectOne(new QueryWrapper<EnergyParamManage>());
        if (paramManage != null) {
            manageVo.setId(paramManage.getId());
            manageVo.setStandardId(paramManage.getStandardId());
            manageVo.setPowerCoal(paramManage.getPowerCoal());
            manageVo.setPowerCarbon(paramManage.getPowerCarbon());
            manageVo.setWaterCoal(paramManage.getWaterCoal());
            manageVo.setWaterCarbon(paramManage.getWaterCarbon());
            manageVo.setGasCoal(paramManage.getGasCoal());
            manageVo.setGasCarbon(paramManage.getGasCarbon());
            manageVo.setSulfur(paramManage.getSulfur());
            manageVo.setDust(paramManage.getDust());
            manageVo.setCarbonEmission(paramManage.getCarbonEmission());
            manageVo.setDiurnalStartTime(paramManage.getDiurnalStartTime());
            manageVo.setDiurnalEndTime(paramManage.getDiurnalEndTime());
            manageVo.setHotMonth(paramManage.getHotMonth());
            manageVo.setColdMonth(paramManage.getColdMonth());
            manageVo.setCreateBy(paramManage.getCreateBy());
            manageVo.setCreateTime(paramManage.getCreateTime());
            manageVo.setUpdateBy(paramManage.getUpdateBy());
            manageVo.setUpdateTime(paramManage.getUpdateTime());
            List<EnergyParamStandard> selectList = energyParamStandardService.getBaseMapper().selectList(Wrappers.<EnergyParamStandard>lambdaQuery()
                    .eq(EnergyParamStandard::getStandardId, paramManage.getStandardId()));
            manageVo.setStandardList(selectList);
        }
        return manageVo;
    }

    @Override
    public boolean saveAndUpdate(EnergyParamManageVo energyParamManage) {
        if (energyParamManage != null) {
            EnergyParamManage manage = new EnergyParamManage();
            manage.setPowerCoal(energyParamManage.getPowerCoal());
            manage.setPowerCarbon(energyParamManage.getPowerCarbon());
            manage.setWaterCoal(energyParamManage.getWaterCoal());
            manage.setWaterCarbon(energyParamManage.getWaterCarbon());
            manage.setGasCoal(energyParamManage.getGasCoal());
            manage.setGasCarbon(energyParamManage.getGasCarbon());
            manage.setSulfur(energyParamManage.getSulfur());
            manage.setDust(energyParamManage.getDust());
            manage.setCarbonEmission(energyParamManage.getCarbonEmission());
            manage.setDiurnalStartTime(energyParamManage.getDiurnalStartTime());
            manage.setDiurnalEndTime(energyParamManage.getDiurnalEndTime());
            manage.setHotMonth(energyParamManage.getHotMonth());
            manage.setColdMonth(energyParamManage.getColdMonth());
            if (energyParamManage.getId() != null) {
                //修改
                manage.setId(energyParamManage.getId());
                this.baseMapper.updateById(manage);
                if (energyParamManage.getStandardList() != null) {
                    energyParamStandardService.getBaseMapper().delete(Wrappers.<EnergyParamStandard>lambdaQuery()
                            .eq(EnergyParamStandard::getStandardId, energyParamManage.getStandardId()));
                    energyParamManage.getStandardList().forEach(standard -> {
                        // 在这里处理每个standard对象
                        standard.setStandardId(energyParamManage.getStandardId());
                        energyParamStandardService.save(standard);
                    });
                }
            } else {
                String formatDateTime2 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
                String standardId = "CS" + formatDateTime2 + RandomUtil.randomString(5);
                //新增
                manage.setStandardId(standardId);
                this.baseMapper.insert(manage);
                if (energyParamManage.getStandardList() != null) {
                    energyParamManage.getStandardList().forEach(standard -> {
                        standard.setStandardId(standardId);
                        energyParamStandardService.save(standard);
                    });
                }
            }
        }
        return true;
    }
}